package com.lyl.servicevod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lyl.commonutils.DateUtils;
import com.lyl.commonutils.R;
import com.lyl.commonutils.redis.RedisConstants;
import com.lyl.commonutils.redis.RedisUtils;
import com.lyl.servicevod.service.VideoService;
import com.lyl.servicevod.utils.AliyunVodSDKUtils;
import com.lyl.servicevod.utils.ConstantVodPropertiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(description="阿里云视频点播前台")

@RestController
@RequestMapping("/vod/api/video")
public class VideoApiController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    private VideoService videoService;
    @ApiOperation("根据视频id获取播放凭证")
    @GetMapping("getPlayAuth/{videoId}")
    //http://localhost:9000/vod/api/video/getPlayAuth/
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {

        //获取阿里云存储相关常量
        String accessKeyId = ConstantVodPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantVodPropertiesUtil.ACCESS_KEY_SECRET;

        //初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);

        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);

        //得到播放凭证
        String playAuth = response.getPlayAuth();

        System.out.println("播放凭证:"+ playAuth);
        // 播放量加1 信息在4号数据库
        RedisUtils.setDataBase(redisTemplate,4);
        // key为当前日期的登录量
        RedisUtils.incr(redisTemplate, DateUtils.formatDate(new Date(),0)+"/"+ RedisConstants.VIDEO_VIEW_NUM,1);

        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }
}